package com.jiezhuo.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jiezhuo.entity.LoginUser;
import com.jiezhuo.entity.cmeduSchool.JzgNewMenu;
import com.jiezhuo.entity.cmeduSchool.Xxdw;
import com.jiezhuo.entity.cmeduSchool.XxdwXq;
import com.jiezhuo.entity.vo.shcool.XxDto;
import com.jiezhuo.handler.RzException;
import com.jiezhuo.service.cmeduSchool.XxdwService;
import com.jiezhuo.service.cmeduSchool.XxdwXqService;
import com.jiezhuo.utils.R;
import com.jiezhuo.utils.SecurityUtils;
import com.jiezhuo.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 服务控制器
 *
 * @author kancy
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2024-06-06 08:59:27
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@Api(tags = "学校系统管理--学校")
@RequestMapping("/xxdw")
@Transactional
public class XxdwController {
    private final XxdwService xxdwService;

    private final XxdwXqService xxdwXqService;


    @ApiOperation("获取学校信息")
    @GetMapping("getSchoolInfo")
    public R getSchoolInfo(@RequestParam Map<String, Object> map) {
        List<Map<String, Object>> schoolInfo = xxdwService.getSchoolInfo(map);
        List<Map<String, Object>> schoolInfoCount = xxdwService.getSchoolInfoCount(map);
        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("schoolInfo", schoolInfo);
        hashMap.put("schoolInfoCount", schoolInfoCount);
        return R.ok().data("schoolInfo", hashMap);
    }

    @ApiOperation("获取学校详细信息-单个获取")
    @GetMapping("getSchoolMaxInfo")
    public R getSchoolMaxInfo(@RequestParam String newXxdm) {
        List<Map<String, Object>> schoolMaxInfo = xxdwService.getSchoolMaxInfo(newXxdm);
        return R.ok().data("schoolMaxInfo", schoolMaxInfo);
    }

    @ApiOperation("获取校区信息")
    @GetMapping("getCampusInfo")
    public R getCampusInfo(@RequestParam Map<String, Object> map) {
        List<Map<String, Object>> CampusInfo = xxdwService.getCampusInfo(map);

        List<Map<String, Object>> schoolInfoCount = xxdwService.getCampusInfoCount(map);
        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("CampusInfo", CampusInfo);
        hashMap.put("schoolInfoCount", schoolInfoCount);

        return R.ok().data("campusInfo", hashMap);
    }

    @ApiOperation("增加学校信息")
    @PostMapping("addSchoolInfo")
    public R addSchoolInfo(@RequestBody XxDto xxDto) {
        // 判断元素 是否存在
        beforeVerify(xxDto);

        // 增加学校开始
        Xxdw xxdw = new Xxdw();
        BeanUtils.copyProperties(xxDto, xxdw);

        // 判断 学校代码 是否存在
        isXxExits(xxDto);

        // 设置属性
        String uuid = UUID.randomUUID().toString();
        xxdw.setNewXxdm(xxDto.getXxdm());
        xxdw.setXxid(uuid);
        xxdw.setOperator(SecurityUtils.getCurrentUserId());
        xxdw.setOperateTime(new Date());
        // 增加学校
        boolean save = xxdwService.save(xxdw);
        // 增加学校结束


        // 判断校区开始
        isXqExist(xxDto);
        // 增加校区
        XxdwXq xxdwXq = new XxdwXq();
        BeanUtils.copyProperties(xxDto, xxdwXq);
        xxdwXq.setOperateTime(new Date());
        xxdwXq.setXqdm(xxDto.getXxdm());
        xxdwXq.setXxdm(xxDto.getXxdm());
        xxdwXq.setXqid(uuid);
        xxdwXq.setXxid(uuid);
        xxdwXq.setNewXxdm(xxDto.getXxdm());
        xxdwXq.setNewXqdm(xxDto.getXqdm());
        xxdwXq.setOperator(SecurityUtils.getCurrentUserId());
        //增加校区
        boolean save1 = xxdwXqService.save(xxdwXq);
        // 判断校区结束

        return R.ok().data("start", save && save1);
    }

    @ApiOperation("修改学校信息")
    @PostMapping("updateSchoolInfo")
    public R updateSchoolInfo(@RequestBody XxDto xxDto) {

        beforeVerify(xxDto);
        // 修改学校开始
        Xxdw xxdw = new Xxdw();
        BeanUtils.copyProperties(xxDto, xxdw);

        xxdw.setOperator(SecurityUtils.getCurrentUserId());
        xxdw.setOperateTime(new Date());
        UpdateWrapper<Xxdw> xxdwUpdateWrapper = new UpdateWrapper<>();
        xxdwUpdateWrapper.eq("NEW_XXDM", xxDto.getXxdm());
        boolean update = xxdwService.update(xxdw, xxdwUpdateWrapper);

//        // 修改校区
//        XxdwXq xxdwXq = new XxdwXq();
//        BeanUtils.copyProperties(xxDto, xxdwXq);
//
//        xxdwXq.setOperateTime(new Date());
//        xxdwXq.setOperator(SecurityUtils.getCurrentUserId());
//
//        UpdateWrapper<XxdwXq> xxdwXqUpdateWrapper = new UpdateWrapper<>();
//        xxdwUpdateWrapper.eq("NEW_XQDM", xxDto.getXxdm());
//        boolean update1 = xxdwXqService.update(xxdwXq, xxdwXqUpdateWrapper);

//&& update1
        return R.ok().data("start", update);
    }


    private void beforeVerify(XxDto xxDto) {
        if (StringUtils.isStringNotExist((xxDto.getXxdm()))) {
            throw new RzException(500, "学校代码必需填写");
        }
        if (StringUtils.isStringNotExist((xxDto.getXxmc()))) {
            throw new RzException(500, "学校全称必需填写");
        }
        if (StringUtils.isStringNotExist((xxDto.getXxjc()))) {
            throw new RzException(500, "学校简称必需填写");
        }
        if (StringUtils.isStringNotExist((xxDto.getXqmc()))) {
            throw new RzException(500, "校区全称必需填写");
        }
        if (StringUtils.isStringNotExist((xxDto.getXqjc()))) {
            throw new RzException(500, "校区简称必需填写");
        }
    }

    private void isXxExits(XxDto xxDto) {
        Xxdw prop = new Xxdw();
        prop.setNewXxdm(xxDto.getXxdm());
        Xxdw one = xxdwService.getOne(new QueryWrapper<Xxdw>(prop));

        if (one != null) {
            throw new RzException(500, "学校已存在");
        }
    }

    private void isXqExist(XxDto xxDto) {
        // 判断 学校代码 是否存在
        XxdwXq propXq = new XxdwXq();
        propXq.setNewXxdm(xxDto.getXxdm());
        XxdwXq oneXq = xxdwXqService.getOne(new QueryWrapper<XxdwXq>(propXq));

        if (oneXq != null) {
            throw new RzException(500, "校区已存在");
        }
    }

    @ApiOperation("学校修改初始化函数")
    @GetMapping("loadUpdateSchoolInfo")
    public R loadUpdateSchoolInfo(@RequestParam String newXxdm) {
        QueryWrapper<Xxdw> queryWrapper = new QueryWrapper();
        queryWrapper.eq("NEW_XXDM", newXxdm);
        List<Xxdw> xxdwList = xxdwService.list(queryWrapper);

        QueryWrapper<XxdwXq> xxdwXqQueryWrapper = new QueryWrapper();
        xxdwXqQueryWrapper.eq("NEW_XQDM", newXxdm);

        List<XxdwXq> xxdwXqList = xxdwXqService.list(xxdwXqQueryWrapper);

        HashMap<String, Object> hashMap = new HashMap<>();

        hashMap.put("xxdwList", xxdwList);
        hashMap.put("xxdwXqList", xxdwXqList);

        return R.ok().data("loadUpdateSchoolInfo", hashMap);
    }

}
